package com.antdesignbackend.backend.activiti.processrecord.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.antdesignbackend.backend.activiti.constant.Constants;
import com.antdesignbackend.backend.activiti.processstay.entity.Processstay;
import com.antdesignbackend.backend.config.WebConfig;
import com.antdesignbackend.backend.form.animalApplication.entity.AnimalApplication;
import com.antdesignbackend.backend.form.animalApplication.service.AnimalApplicationService;
import com.antdesignbackend.backend.form.animalDaily.entity.AnimalDaily;
import com.antdesignbackend.backend.form.animalDaily.service.AnimalDailyService;
import com.antdesignbackend.backend.form.animalOrigin.entity.AnimalOrigin;
import com.antdesignbackend.backend.form.animalOrigin.service.AnimalOriginService;
import com.antdesignbackend.backend.form.businessAccount.entity.BusinessAccount;
import com.antdesignbackend.backend.form.businessAccount.service.BusinessAccountService;
import com.antdesignbackend.backend.form.certificateA.entity.CertificateA;
import com.antdesignbackend.backend.form.certificateA.service.CertificateAService;
import com.antdesignbackend.backend.form.certificateB.entity.CertificateB;
import com.antdesignbackend.backend.form.certificateB.service.CertificateBService;
import com.antdesignbackend.backend.form.farmersDaily.entity.FarmersDaily;
import com.antdesignbackend.backend.form.farmersDaily.service.FarmersDailyService;
import com.antdesignbackend.backend.form.investors.entity.Investors;
import com.antdesignbackend.backend.form.investors.service.InvestorsService;
import com.antdesignbackend.backend.form.pigAccount.entity.PigAccount;
import com.antdesignbackend.backend.form.pigAccount.service.PigAccountService;
import com.antdesignbackend.backend.form.report.entity.Report;
import com.antdesignbackend.backend.form.report.service.ReportService;
import com.antdesignbackend.backend.form.saleDeclarationA.entity.SaleDeclarationA;
import com.antdesignbackend.backend.form.saleDeclarationA.service.SaleDeclarationAService;
import com.antdesignbackend.backend.form.saleDeclarationB.entity.SaleDeclarationB;
import com.antdesignbackend.backend.form.saleDeclarationB.service.SaleDeclarationBService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import com.antdesignbackend.backend.common.web.BaseController;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.activiti.processrecord.entity.Processrecord;
import com.antdesignbackend.backend.activiti.processrecord.entity.entityVo.ProcessrecordVo;
import com.antdesignbackend.backend.activiti.processrecord.service.ProcessrecordService;

/**
 * 审批记录
 *
 * @author 不二
 * @email processrecord
 * @date 2020-08-13 09:54:16
 */

@Controller
@RequestMapping("/processrecord")
@Slf4j
public class ProcessrecordController extends BaseController{

	private static final String FUN_NAME = "审批记录";//功能名称

	@Autowired
	private ProcessrecordService processrecordService;
	@Autowired
	private ReportService reportService;
	@Autowired
	private AnimalDailyService animalDailyService;
	@Autowired
	private FarmersDailyService farmersDailyService;
	@Autowired
	private PigAccountService pigAccountService;
	@Autowired
	private CertificateBService certificateBService;
	@Autowired
	private CertificateAService certificateAService;
	@Autowired
	private AnimalOriginService animalOriginService;
	@Autowired
	private BusinessAccountService businessAccountService;
	@Autowired
	private AnimalApplicationService animalApplicationService;
	@Autowired
	private InvestorsService investorsService;
	@Autowired
	private SaleDeclarationAService saleDeclarationAService;
	@Autowired
	private SaleDeclarationBService saleDeclarationBService;

	@GetMapping("/index")
	@AuthToken
	public String index() {
		return "activiti/processrecord/index";
	}


	@PostMapping("/tableSearch")
	@ResponseBody
	ReturnVO tableSearch(@RequestBody ProcessrecordVo processrecordVo) throws SecurityException, IllegalArgumentException{
		ReturnVO result = new ReturnVO();
		List<ProcessrecordVo> list = new ArrayList<>();
		QueryWrapper<ProcessrecordVo> wrapper = new QueryWrapper<>();
//		wrapper.eq("userId",getLoginUserId());
		wrapper.orderByAsc("Form","formId","OrderId");
		//过滤
		if (processrecordVo.getProcessId() != null){
			wrapper.eq("processId",processrecordVo.getProcessId());
		}
		if (processrecordVo.getUserName() != null && !processrecordVo.getUserName().equals("")){
			wrapper.like("userName",processrecordVo.getUserName());
		}
		if (processrecordVo.getStatus() != null && !processrecordVo.getStatus().equals("")){
			wrapper.eq("status",processrecordVo.getStatus());
		}
		int total = processrecordService.count(wrapper);
		if (total > 0){
			list =  processrecordService.search(wrapper,new Page<ProcessrecordVo>(processrecordVo.getPageIndex(),processrecordVo.getPageSize()));
		}
		result.setTotal(total);
		result.setData(list);
		return result;

	}

	@GetMapping("/getById/{id}")
	@ResponseBody
	public	Processrecord getById(@PathVariable(name = "id") String id) {
		Processrecord obj = processrecordService.getById(id);
		return obj;
	}

	@PostMapping("/save")
	@ResponseBody
	public ReturnVO save(@RequestBody Processrecord processrecord){
		ReturnVO result = new ReturnVO();
		if(processrecordService.save(processrecord)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	@PostMapping("update")
	@ResponseBody
	public ReturnVO updateById(@RequestBody Processrecord processrecord){
		ReturnVO result = new ReturnVO();
		if(processrecordService.updateById(processrecord)){
			result.setMessage("保存成功！");
			result.setCode("200");
		}else{
			result.setMessage("保存失败！");
			result.setCode("500");
		}
		return result;
	}

	/**
     * 根据id删除
     */
	@DeleteMapping("/deleteById/{id}")
	@ResponseBody
	public ReturnVO deleteById(@PathVariable Serializable id) {
		ReturnVO result = new ReturnVO();
		if (processrecordService.removeById(Integer.parseInt(id.toString())) == true) {
			result.setMessage("删除成功");
		} else {
			result.setMessage("删除失败");
			result.setCode("0");
		}
		return result;
	}

	/**
	 * 根据审批记录获得对应的表单
	 */
	@PostMapping("/getFormById")
	@ResponseBody
	public ReturnVO getFormById(Integer id) {
		ReturnVO returnVO = new ReturnVO();
		Object object = new Object();
		//获得记录
		Processrecord processrecord = processrecordService.getById(id);
		if (Constants.REPORT.equals(processrecord.getForm())){
			Report report = reportService.getById(processrecord.getFormId());
			if (report != null){
				report.setPhoto(WebConfig.host+"images/"+report.getPhoto());
				object = report;
			}
		}
		if (Constants.ANIMALDAILY.equals(processrecord.getForm())){
			AnimalDaily animalDaily = animalDailyService.getById(processrecord.getFormId());
			if (animalDaily != null){
				animalDaily.setInspectionPhoto(WebConfig.host+"images/"+animalDaily.getInspectionPhoto());
				object = animalDaily;
			}
		}
		if (Constants.FARMERSDAILY.equals(processrecord.getForm())){
			FarmersDaily farmersDaily = farmersDailyService.getById(processrecord.getFormId());
			object=farmersDaily;
		}
		if (Constants.PIGACCOUNT.equals(processrecord.getForm())){
			PigAccount pigAccount = pigAccountService.getById(processrecord.getFormId());
			if (pigAccount != null){
				pigAccount.setRecordForm(WebConfig.host+"images/"+pigAccount.getRecordForm());
				object = pigAccount;
			}
		}
		if (Constants.CERTIFICATEB.equals(processrecord.getForm())){
			CertificateB certificateB = certificateBService.getById(processrecord.getFormId());
			if (certificateB != null){
				certificateB.setPhoto(WebConfig.host+"images/"+certificateB.getPhoto());
				object = certificateB;
			}
		}
		if (Constants.CERTIFICATEA.equals(processrecord.getForm())){
			CertificateA certificateA = certificateAService.getById(processrecord.getFormId());
			if (certificateA != null){
				certificateA.setPhoto(WebConfig.host+"images/"+certificateA.getPhoto());
				object = certificateA;
			}
		}
		if (Constants.ANIMALORIGIN.equals(processrecord.getForm())){
			AnimalOrigin animalOrigin = animalOriginService.getById(processrecord.getFormId());
			if (animalOrigin != null){
				animalOrigin.setOtherPhoto(WebConfig.host+"images/"+animalOrigin.getOtherPhoto());
				animalOrigin.setAnimalPhoto(WebConfig.host+"images/"+animalOrigin.getAnimalPhoto());
				object = animalOrigin;
			}
		}
		if (Constants.BUSINESSACCOUNT.equals(processrecord.getForm())){
			BusinessAccount businessAccount = businessAccountService.getById(processrecord.getFormId());
			if (businessAccount != null){
				businessAccount.setPhoto(WebConfig.host+"images/"+businessAccount.getPhoto());
				object = businessAccount;
			}
		}
		if (Constants.ANIMALAPPLICATION.equals(processrecord.getForm())){
			AnimalApplication animalApplication = animalApplicationService.getById(processrecord.getFormId());
			if (animalApplication != null){
				animalApplication.setReportPhoto(WebConfig.host+"images/"+animalApplication.getReportPhoto());
				animalApplication.setLicensePhoto(WebConfig.host+"images/"+animalApplication.getLicensePhoto());
				object = animalApplication;
			}
		}
		if (Constants.INVESTORS.equals(processrecord.getForm())){
			Investors investors = investorsService.getById(processrecord.getFormId());
			if (investors != null){
				investors.setPhoto(WebConfig.host+"images/"+investors.getPhoto());
				object = investors;
			}
		}
		if (Constants.SALEDECLARATIONA.equals(processrecord.getForm())){
			SaleDeclarationA saleDeclarationA = saleDeclarationAService.getById(processrecord.getFormId());
			if (saleDeclarationA != null){
				saleDeclarationA.setAnimalPhoto(WebConfig.host+"images/"+saleDeclarationA.getAnimalPhoto());
				saleDeclarationA.setCarPhoto(WebConfig.host+"images/"+saleDeclarationA.getCarPhoto());
				saleDeclarationA.setPigCarPhoto(WebConfig.host+"images/"+saleDeclarationA.getPigCarPhoto());
				saleDeclarationA.setCertificatePhoto(WebConfig.host+"images/"+saleDeclarationA.getCertificatePhoto());
				object = saleDeclarationA;
			}
		}
		if (Constants.SALEDECLARATIONB.equals(processrecord.getForm())){
			SaleDeclarationB saleDeclarationB = saleDeclarationBService.getById(processrecord.getFormId());
			if (saleDeclarationB != null){
				saleDeclarationB.setAnimalPhoto(WebConfig.host+"images/"+saleDeclarationB.getAnimalPhoto());
				saleDeclarationB.setCarPhoto(WebConfig.host+"images/"+saleDeclarationB.getCarPhoto());
				saleDeclarationB.setPigCarPhoto(WebConfig.host+"images/"+saleDeclarationB.getPigCarPhoto());
				saleDeclarationB.setCertificatePhoto(WebConfig.host+"images/"+saleDeclarationB.getCertificatePhoto());
				saleDeclarationB.setReportPhoto(WebConfig.host+"images/"+saleDeclarationB.getReportPhoto());
				saleDeclarationB.setAgriculturePhoto(WebConfig.host+"images/"+saleDeclarationB.getAgriculturePhoto());
				object = saleDeclarationB;
			}
		}
		returnVO.setData(object);
		return returnVO;
	}

}
